﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZSPG_YYBCJFHInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            if (!base.Page.IsPostBack) {
                Bind();
            }
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            CustomGridList1.RowDeleting += new GridViewDeleteEventHandler(CustomGridList1_RowDeleting);
            Button btnsearch = (Button)FindControl("Button1");
            btnsearch.Click += new EventHandler(Go_Search);
        }

        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Bind()
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            CustomGridList1.RowDataBound += new GridViewRowEventHandler(CustomGridList1_RowDataBound);
            DataTable dt = _EluZSDataProvider.GetPGFHXX("1", GetSqlWhere());
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.DataKeyNames = new string[] { "ID" };
            CustomGridList1.DataBind();
            
        }

        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected string GetSqlWhere()
        {
            string sqlWhere = string.Empty;
            TextBox txtCQR = (TextBox)FindControl("txtCQR");
            TextBox txtCZR = (TextBox)FindControl("txtCZR");
            TextBox txtFWZL = (TextBox)FindControl("txtFWZL");
            string strCQR = txtCQR.Text;
            string strCZR = txtCZR.Text;
            string strFWZL = txtFWZL.Text;
            string PID = base.Page.Request["PID"];
            if (!string.IsNullOrEmpty(PID))
            {
                sqlWhere += " and z.pid='" + PID + "'";
            }
            if (!string.IsNullOrEmpty(strCQR))
            {
                sqlWhere += " and p.产权人= '" + strCQR + "'";
            }
            if (!string.IsNullOrEmpty(strCZR))
            {
                sqlWhere += " and p.承租人='" + strCZR + "'";
            }
            if (!string.IsNullOrEmpty(strFWZL))
            {
                sqlWhere += " and p.房屋坐落 like '%" + strFWZL + "%'";
            }
            sqlWhere += " order by z.addtime desc ";
            return sqlWhere;
        }
        protected void CustomGridList1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string strZSPID = ((HiddenField)e.Row.FindControl("HiddenField1")).Value;
                if (_EluZSDataProvider.GetBZSRXY(strZSPID))
                {
                    e.Row.Cells[12].Text = "删除";
                    e.Row.Cells[12].Attributes.Add("style", "cursor:pointer;color:gray;");
                    e.Row.Cells[12].Attributes.Add("onclick", "javascript:alert('该户已做协议，不允许删除！');");
                }
            }
        }
        protected void CustomGridList1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            string strid = CustomGridList1.DataKeys[e.RowIndex].Value.ToString();
            bool del = _EluZSDataProvider.DeleteXmByID("ZS_ZZBCJFENHU", "id", strid);
            if (del)
            {
                Bind();
                base.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功！')", true);
            }
        }
    }
}


